﻿#include <iostream>
#include <vector>
using namespace std;


class Solution
{
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int m, n;
public:
    // 417. 太平洋大西洋水流问题
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights)
    {
        vector<vector<int>> ret;
        m = heights.size(), n = heights[0].size();
        vector<vector<bool>> PO(m, vector<bool>(n));
        vector<vector<bool>> AO(m, vector<bool>(n));

        for (int j = 0; j < n; j++)
            dfs(heights, 0, j, PO);
        for (int i = 0; i < m; i++)
            dfs(heights, i, 0, PO);

        for (int j = 0; j < n; j++)
            dfs(heights, m - 1, j, AO);
        for (int i = 0; i < m; i++)
            dfs(heights, i, n - 1, AO);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (PO[i][j] && AO[i][j])
                    ret.push_back({ i, j });
        return ret;
    }

    void dfs(vector<vector<int>>& heights, int i, int j, vector<vector<bool>>& vis)
    {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && heights[i][j] <= heights[x][y])
                dfs(heights, x, y, vis);
        }
    }
};

int main()
{

    return 0;
}